﻿using ComLib.ComFun;
using ComLib.ConfigService;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;

namespace PrintExcelSetting
{
    public class PaperPrinterOption : XmlSerializeBase
    {
        [XmlIgnore]
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        public PaperPrinterOption()
        {

        }

        /// <summary>
        /// 回执打印机名称
        /// </summary>
        private string _printer = "POS80";
        public string Printer
        {
            get { return _printer; }
            set { _printer = value; }
        }

        /// <summary>
        /// 使用单位
        /// </summary>
        private string _department = "公安局出入境";
        public string Department
        {
            get { return _department; }
            set { _department = value; }
        }

        /// <summary>
        /// 设备名称
        /// </summary>
        private string _deviceName = "DevNo.1";
        public string DeviceName
        {
            get { return _deviceName; }
            set { _deviceName = value; }
        }

        /// <summary>
        /// 设备编号
        /// </summary>
        private string _deviceNo = "001";
        public string DeviceNo
        {
            get { return _deviceNo; }
            set { _deviceNo = value; }
        }

        /// <summary>
        /// 是否打印头像
        /// </summary>
        private bool _isPrintPerson = false;
        public bool IsPrintPerson
        {
            get { return _isPrintPerson; }
            set { _isPrintPerson = value; }
        }

        /// <summary>
        /// 超时时间
        /// </summary>
        private int _timeout = 8;
        public int Timeout
        {
            get { return _timeout; }
            set { _timeout = value; }
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            PaperPrinterOption obj = (PaperPrinterOption)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this._printer = obj._printer;
                this._department = obj.Department;
                this.DeviceName = obj.DeviceName;
                this.DeviceNo = obj.DeviceNo;
                this.IsPrintPerson = obj.IsPrintPerson;
                this.Timeout = obj.Timeout;
                this._nVer = obj._nVer;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("表格打印参数: ");
            FillFieldValueStringDelegate FillString =
              (string field, object val) =>
              {
                  builder.Append("[");
                  builder.Append(field);
                  builder.Append("]: ");
                  builder.Append(val.ToString() + "; ");
              };
            FillString("打印机名称:", _printer);
            FillString("使用部门:", _department);
            FillString("设备名称:", _deviceName);
            FillString("设备编号:", _deviceNo);
            FillString("超时时间:", _timeout);
            FillString("是否打印头像:", _isPrintPerson ? "是" : "否");
            return builder.ToString();
        }
    }
}
